package com.funcas.pcloud.upms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.funcas.pcloud.common.util.IdWorker;
import com.funcas.pcloud.upms.entity.Unit;
import com.funcas.pcloud.upms.mapper.OperatingRecordMapper;
import com.funcas.pcloud.upms.mapper.UnitMapper;
import com.funcas.pcloud.upms.service.IUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author funcas
 * @version 1.0
 * @date 2018年04月26日
 */
@Service("organizationService")
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class UnitServiceImpl implements IUnitService {

    @Autowired
    private UnitMapper unitMapper;
    @Autowired
    private OperatingRecordMapper operatingRecordMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = "unitCache", key="#entity.id")
    public void saveOrg(Unit entity) {
        if(entity.getId() != null){
            unitMapper.updateById(entity);
        }else{
            entity.setId(IdWorker.getId());
            unitMapper.insert(entity);
        }

    }

    @Override
    @Cacheable(value = "unitCache", key = "#unitId + ''")
    public Unit getOrganizationById(Long id) {
        return unitMapper.selectById(id);
    }

    @Override
    public List<Unit> findLeaveUnit() {
        return unitMapper.selectList(new QueryWrapper<Unit>().eq("leaf", 1));
    }



}
